package app.auto.runner.clip;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import java.util.Map;

import app.auto.AndroidInstance;
import app.auto.AndroidInto;
import app.auto.runner.base.BroadcastBuilder;
import app.auto.runner.base.intf.MapBuilder;

public class ClipUtil extends AndroidInto {

    public String copytask_value = "copy";
    public String pastetask_value = "paste";

    public String getCopytask_value() {
        return copytask_value;
    }

    public String getPastetask_value() {
        return pastetask_value;
    }


    public String task_name = "task";
    private String value_name = "value";

    public String paste() {

        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

// 获取剪贴板的剪贴数据集
        ClipData clipData = clipboard.getPrimaryClip();

        if (clipData != null && clipData.getItemCount() > 0) {
            // 从数据集中获取（粘贴）第一条文本数据
            CharSequence text = clipData.getItemAt(0).getText();
            return (String) text.toString();
        }
        return "";
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        String task = (String) ((Map) mp).get(task_name);
        if (copytask_value.equals(task)) {
            copy((String) ((Map) mp).get(value_name));
        } else if (pastetask_value.equals(task)) {
            String data = paste();
            BroadcastBuilder.build().sendAction(getClass().getSimpleName(), getContext(), MapBuilder.build().add("data", data), BroadcastBuilder.Starttype.broadcast);
        }
        return null;
    }

    public void copy(String text) {// 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

// 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, text);

// 把数据集设置（复制）到剪贴板
        clipboard.setPrimaryClip(clipData);

    }
}
